package com.company.project.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.company.project.common.utils.DataResult;
import com.company.project.common.utils.Func;
import com.company.project.common.utils.StringUtil;
import com.company.project.entity.SysUser;
import com.company.project.entity.TSeatsEntity;
import com.company.project.mapper.SysUserMapper;
import com.company.project.mapper.TSeatsMapper;
import com.company.project.service.HttpSessionService;
import org.aspectj.weaver.ast.Var;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.company.project.mapper.TBacklistMapper;
import com.company.project.entity.TBacklistEntity;
import com.company.project.service.TBacklistService;

import javax.annotation.Resource;
import java.util.regex.Pattern;


@Service("tBacklistService")
public class TBacklistServiceImpl extends ServiceImpl<TBacklistMapper, TBacklistEntity> implements TBacklistService {

    @Resource
    private HttpSessionService httpSessionService;

    @Autowired
    public TSeatsMapper tSeatsMapper;

    @Autowired
    public TBacklistMapper backlistMapper;

    @Autowired
    public SysUserMapper userMapper;

    @Override
    public DataResult addBacklist(TBacklistEntity tBacklist) {
        try {
            if(!phoneValidate(tBacklist.getPhoneWH()))return DataResult.fail("添加黑名单失败，号码格式异常。");

            Long integer = backlistMapper.selectCount(Wrappers.<TBacklistEntity>lambdaQuery()
                    .eq(TBacklistEntity::getPhone, tBacklist.getPhoneWH()));
            if (integer>0) {
                return DataResult.fail("添加黑名单失败，此号已经在黑名单中。");
            }
            if (Func.isEmpty(tBacklist.getPhoneWH())) {
                return DataResult.fail("添加黑名单失败，黑名单不能为空。");
            }
            //通过access_token拿userId
            String userId = httpSessionService.getCurrentUserId();
            SysUser sysUser = userMapper.selectById(userId);

            TSeatsEntity tSeats = tSeatsMapper.selectOne(Wrappers.<TSeatsEntity>lambdaQuery()
                    .eq(TSeatsEntity::getAccountid, userId));
            //公司名称 添加账号
            if(!Func.isNull(tSeats)){
                tBacklist.setUserid(sysUser.getUsername());
                tBacklist.setCompany(tSeats.getCorporatename());
            }
            tBacklist.setPhone(tBacklist.getPhoneWH());
            boolean save = this.save(tBacklist);
            return DataResult.success(save);
        } catch (Exception e) {
            e.printStackTrace();
            return DataResult.fail("添加黑名单失败，请联系管理员！");
        }
    }
    public boolean phoneValidate(String phoneNumber){
        if (phoneNumber != null && !phoneNumber.isEmpty()){
            boolean matches = Pattern.matches(
                    "^1[3-9]{1}\\d{9}$|" +
                            "^0\\d{3}-\\d{8}$|" +
                            "^0\\d{2}-\\d{8}$", phoneNumber);
            return matches;
        }
        return false;
    }
}